<?php
/**
 * Created by PhpStorm.
 * User: coolinmind
 * Date: 14/01/2019
 * Time: 17:41
 */

namespace App\Services\Cargo\Cargo;


use App\Models\Cargo\Cargo;
use App\Models\Cargo\CommonCargo;
use App\Models\User\User;
use App\Services\Fund\FundAccount\FundAccountLogic;
use DB;

class CargoLogic
{
    /**
     * 发布货物
     * @param $data
     * @return array
     */
    public function addCargo($data)
    {
        $cargoData = pick_array($data, [
            'start_addr',
            'start_addr_detail',
            'end_addr',
            'end_addr_detail',
            'receiver_name',
            'receiver_phone',
            'type',
            'weight',
            'volume',
            'car_lenght',
            'car_type',
            'price',
            'remark',
            'loading_time',
            'user_id',
            'is_common',
            'start_province_id',
            'start_city_id',
            'start_county_id',
            'end_province_id',
            'end_city_id',
            'end_county_id'
        ]);
        $cargoData['created_at'] = date('Y-m-d H:i:s', NOW);
        $cargoData['updated_at'] = date('Y-m-d H:i:s', NOW);

        //支付保证金准备数据
        $payPreData = pick_array($data, [
            'user_id',
            'deposit',
            'pay_pass',
            'pay_type',
            'create_ip'
        ]);
        DB::beginTransaction();
        try {
            if ($data['is_common']) {
                $this->addCommonCargo([
                    'user_id' => $data['user_id'],
                    'addr' => $data['end_addr'],
                    'name' => $data['receiver_name'],
                    'phone' => $data['receiver_phone'],
                    'province_id' => $data['end_province_id'],
                    'city_id' => $data['end_city_id'],
                    'county_id' => $data['end_county_id'],
                ]);
            }
            $res = Cargo::insertGetId($cargoData);

            //更新发货数
            User::where(['id' => $data['user_id']])->increment('release_num');

            $payPreData['waybill_id'] = $res;
            //支付保证金
            $payRes = (new FundAccountLogic())->payDeposit($payPreData);
            if ($payRes['status'] == SUCCESS_STATUS) {
                DB::commit();
                return auto_success($res, '发布成功');
            }else{
                DB::rollBack();
                return auto_failure(null, $payRes['msg']);
            }
        } catch (\Exception $e) {
            DB::rollBack();
            return auto_failure($e->getMessage(), '数据库异常');
        }
    }

    /**
     * 修改货物信息
     * @param $data
     */
    public function changeComCargo($data)
    {
        $cargoMod = CommonCargo::where(['user_id' => $data['user_id'], 'id' => $data['id']])->first();
        if(!$cargoMod)
            return auto_failure($cargoMod, '该信息不存在');
        $cargoMod->addr = $data['addr'];
        $cargoMod->name = $data['name'];
        $cargoMod->phone = $data['phone'];
        $cargoMod->province_id = $data['province_id'];
        $cargoMod->city_id = $data['city_id'];
        $cargoMod->county_id = $data['county_id'];
        try {
            $res = $cargoMod->save();
            return $res ?
                auto_success($res, '修改成功') :
                auto_failure($res, '修改失败');
        } catch (\Exception $e) {
            return auto_failure($e->getMessage());
        }
    }

    /**
     * 获取我的货源列表
     * @param $data
     * @return array
     */
    public function getCargoList($data)
    {
        $_where = [
          'user_id' => $data['user_id'],
          'status' => $data['status']
        ];
        $res = Cargo::where($_where)->where('status', '<>', 2)
            ->orderBy('updated_at', 'desc')
            ->paginate(6, null, null, $data['page']);
        return $res ?
            auto_success($res, '获取列表成功') :
            auto_failure($res, '获取列表失败');
    }

    /**
     * 添加常发货源
     * @param $data
     * @return array
     */
    public function addCommonCargo($data)
    {
        $data['created_at'] = date('Y-m-d H:i:s', NOW);
        $data['updated_at'] = date('Y-m-d H:i:s', NOW);
        $res = CommonCargo::insert($data);
        return $res ?
            auto_success($res,'添加成功') :
            auto_failure($res, '添加失败');
    }

    /**
     * 获取常发货源列表
     * @param $data
     * @return array
     */
    public function getComCargoList($data)
    {
        $res = CommonCargo::where('user_id', $data['user_id'])->paginate(6, null, null, $data['page']);
        return $res ?
            auto_success($res,'获取列表成功') :
            auto_failure($res, '获取列表失败');
    }

    /**
     * 删除失败
     * @param $data
     * @return array
     */
    public function delComCargo($data)
    {
        $res = CommonCargo::where('id', $data['id'])->delete();
        return $res ?
            auto_success($res, '删除成功') :
            auto_failure($res, '删除失败');
    }

    /**
     * 获取列表
     * @param $data
     * @return array
     */
    public function findCargos($data)
    {
        $_where = [];
        $_where[] = ['status', '=', 1];
        foreach ($data as $k => $v) {
            if($k !== 'page' && $v)
                $_where[] = [$k, '=', $v];
        }
        $res = Cargo::where($_where)->with('user')->paginate(6, null, null, $data['page']);
        return $res ?
            auto_success($res, '获取列表成功') :
            auto_failure($res, '获取列表失败');
    }

    /**
     * 获取我的货源列表
     * @param $data
     * @return array
     */
    public function backCargoList($data)
    {
        $res = Cargo::orWhere('receiver_name', 'like', '%' . $data['keywords'] . '%')
            ->orWhere('receiver_phone', 'like', '%' . $data['keywords'] . '%')
            ->paginate(10);
        return auto_return($res, "获取列表成功", "获取列表失败");
    }

    /**
     * 根据id获取货物详情
     * @param $data
     */
    public function getCargoDetail($data)
    {
        $info = Cargo::where(['id' => $data['id']])->first();
        return auto_return($info);
    }
}
